<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button class="btn" id="btn">点击发送</button>

    <script>
        // 接口地址 http://ajax-api.itheima.net/api/books
        document.querySelector('.btn').addEventListener('click', function () {
            const p = new Promise((resolve,reject) => {
                 // 创建对象
            const xht = new XMLHttpRequest()
            // 初始化
            xht.open('GET','http://ajax-api.itheima.net/api/books')
            // 发送
            xht.send()
            // 处理响应结果
            xht.onreadystatechange = function(){
                if(xht.readyState === 4){
                    if(xht.status >= 200 && xht.status < 300){
                        resolve(xht.response)
                    }else{
                        reject(xht.status);
                    }   
                }
            }
            })
           p.then(value => {
            console.log(value);
           } , reason=> {
            console.warn(reason);
           })
        })
        
    </script>
</body>
</html>